****APPENDIX FIGURE F5****

cd "$main_dir/output/figures/appendix_f"

graph set window fontface "Palatino"

global basic 	marital edu
global econ	    employment_status occupation income imputed_income
global design 	survey_year age language
global polit    ideology party_member

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/shp_final_panel.dta"

gen dummy = 1 if trust_fed_govt_med!=.
bys idpers: egen total_obs = total(dummy)

xtset idpers survey_year

cap drop sample
gen sample = 1 if live_switz==1 & total_obs>4 & total_obs!=. & age>=15 & age!=.

//--------------------------------------------------------
//Run grid search
//--------------------------------------------------------

mat results = J(61, 3, .)
mat colnames results = lambda beta rss

local i = 1
local j = -3
local varlist wgrowth_rate_neg3 wgrowth_rate_neg2_9 wgrowth_rate_neg2_8 wgrowth_rate_neg2_7 wgrowth_rate_neg2_6 wgrowth_rate_neg2_5 wgrowth_rate_neg2_4 wgrowth_rate_neg2_3 wgrowth_rate_neg2_2 wgrowth_rate_neg2_1 wgrowth_rate_neg2 wgrowth_rate_neg1_9 wgrowth_rate_neg1_8 wgrowth_rate_neg1_7 wgrowth_rate_neg1_6 wgrowth_rate_neg1_5 wgrowth_rate_neg1_4 wgrowth_rate_neg1_3 wgrowth_rate_neg1_2 wgrowth_rate_neg1_1 wgrowth_rate_neg1 wgrowth_rate_neg0_9 wgrowth_rate_neg0_8 wgrowth_rate_neg0_7 wgrowth_rate_neg0_6 wgrowth_rate_neg0_5 wgrowth_rate_neg0_4 wgrowth_rate_neg0_3 wgrowth_rate_neg0_2 wgrowth_rate_neg0_1 wgrowth_rate_0 wgrowth_rate_0_1 wgrowth_rate_0_2 wgrowth_rate_0_3 wgrowth_rate_0_4 wgrowth_rate_0_5 wgrowth_rate_0_6 wgrowth_rate_0_7 wgrowth_rate_0_8 wgrowth_rate_0_9 wgrowth_rate_1 wgrowth_rate_1_1 wgrowth_rate_1_2 wgrowth_rate_1_3 wgrowth_rate_1_4 wgrowth_rate_1_5 wgrowth_rate_1_6 wgrowth_rate_1_7 wgrowth_rate_1_8 wgrowth_rate_1_9 wgrowth_rate_2 wgrowth_rate_2_1 wgrowth_rate_2_2 wgrowth_rate_2_3 wgrowth_rate_2_4 wgrowth_rate_2_5 wgrowth_rate_2_6 wgrowth_rate_2_7 wgrowth_rate_2_8 wgrowth_rate_2_9 wgrowth_rate_3
foreach var of local varlist {
	
	qui reghdfe trust_fed_govt_med `var' if sample==1, absorb(canton $basic $design $econ $polit idpers) cluster(idhous yob)
	cap drop z*
	local list trust_fed_govt_med `var'
	foreach x of local list {
		qui egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_fed_govt_med z`var' if e(sample)==1, absorb(canton $basic $design $econ $polit idpers) cluster(idhous yob)
	mat results[`i', 1] = `j'
	mat results[`i', 2] = e(b)[1,1]
	mat results[`i', 3] = e(rss)
	local ++i
	local j = `j' + 0.1
	
	di "`var' done"
}

matrix list results
svmat double results, names(col)
keep lambda beta rss
drop if lambda==.	

//--------------------------------------------------------
//Create Figure F5
//--------------------------------------------------------

scatter rss lambda, graphregion(color(white)) bgcolor(white) title("RSS", color(black)) ///
	ylabel(,labs(small) format(%9.2f)) xlabel(,labs(small)) xtitle("{&lambda}", margin(small)) ytitle("RSS", margin(small)) ///
	note("Note: each point represents a separate regression RSS per value of {&lambda} used to calculate growth experience weights. The grid search" "ranges from -3 to 3 over increments of 0.1.", size(vsmall))
	graph export "figure_f5_a.png", replace
																
scatter beta lambda, graphregion(color(white)) bgcolor(white) title("Coefficients", color(black)) ///
	ylabel(,labs(small) format(%9.2f)) xlabel(,labs(small)) xtitle("{&lambda}", margin(small)) ytitle("{&beta}", margin(small)) ///
	note("Note: each point represents a separate regression {&beta} per value of {&lambda} used to calculate growth experience weights. The grid search" "ranges from -3 to 3 over increments of 0.1.", size(vsmall))
	graph export "figure_f5_b.png", replace
	
